<?
require_once("cms/core/action/AbstractListAction.php");
require_once("cms/download/om/DownloadRevisionPeer.php");
require_once("cms/core/om/BinaryFilePeer.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating downloads");
        $c = new Criteria();
        $c->add(DownloadPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);
        $c->add(DownloadPeer::ACTIVE, true);
        try {
            DownloadPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating downloads");
        $c = new Criteria();
        $c->add(DownloadPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);
        $c->add(DownloadPeer::ACTIVE, false);
        try {
            DownloadPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting downloads");
        $c = new Criteria();
        $c->add(DownloadRevisionPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            $revisions = DownloadRevisionPeer::doSelect($c, $conn);
            foreach($revisions as $revision) {
                DownloadRevisionPeer::doDelete($revision, $conn);
                BinaryFilePeer::doDelete($revision->getBinaryFile(), $conn);
            }

            $c = new Criteria();
            $c->add(DownloadPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);

            DownloadPeer::doDelete($c, $conn);

            $conn->commit();

            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));

        } catch(PropelException $e) {
            $conn->rollback();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e->getMessage());
        }

    }


}
?>